package com.by.controller;


import cn.hutool.core.bean.BeanUtil;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.query.UserQuery;
import com.by.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/v1")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @ApiImplicitParam(name = "account", paramType = "query", value = "用户名", dataTypeClass = String.class, required = true)
    @ApiOperation("账号唯一检查")
    @GetMapping("/user/check")
    public ResultBean checkUser(@RequestParam(name = "account", required = true) String account) {
        int rs = userService.queryUserByAccount(account);
        String tmp = rs == 1 ? "no" : "yes";
        return success("账号唯一检查", tmp);
    }


    @ApiOperation("用户列表")
    @PostMapping("/user/query")
    public ResultBean query(@RequestBody UserQuery query) {
        Map params = BeanUtil.beanToMap(query);
        PageBean bean = userService.query(params);
        return success("用户列表", bean);
    }


}
